﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class ManagerController : BaseController<manager>
    {
        //
        // GET: /Manager/  
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<manager> bll
        {
            get
            {
                return new ManagerBll();
            }

        }

        ManagerStoreBll managerStoreBll = new ManagerStoreBll();


        public partial class manager1
        {

            public string username { get; set; }
            public string password { get; set; }
            public string RealName { get; set; }
            public string userPhone { get; set; }
            public string storeIDs { get; set; }
        }
        //加载数据列表 查询方法
        public override ActionResult Get(int page, int rows, manager model)
        {
            int count;
            var list = bll.Search(model, x => true, true, x => x.userid, rows, page, out count).ToList().Select(x => new
            {
                x.userid,
                x.username,
                x.userPhone,
                x.RealName,
                x.password,

                state = x.state == 1 ? "在职" : "离职",
            }).ToList();
            return Json(new { total = count, rows = list });
        }

        //新增用户  添加多个店铺的时候  用这个
        [HttpPost]

        public ActionResult Save22(manager1 model1)
        {
            managerstore msModel = new managerstore();
            manager model = new manager();
            string storeid;
            string[] strr = model1.storeIDs.Split(',');

            string msg = "";
            int result = 0;
            int jieguo = 1;
            string code = model1.username;
            string pwd = model1.password;

            if (model1.username != "" && model1.password != "" && model1.username != null && model1.password != null)
            {
                var count = bll.Search(x => x.username == code).Count();
                //判断该用户是否存在
                if (count == 0)
                {
                    model.password = MD5Encrypt32(pwd);
                    model.state = 1;
                    model.RealName = model1.RealName;
                    model.username = model1.username;
                    model.userPhone = model1.userPhone;

                    for (int i = 0; i < strr.Length; i++)
                    {
                        storeid = strr[i];
                        msModel.storeid = storeid;
                        msModel.managerCode = model.userid;
                        msModel.areaCode = 11001;
                        if (managerStoreBll.Search(x => x.storeid == msModel.storeid).Count() != 0)
                        {

                            jieguo = 0;
                            msg = "该店铺已有相关理货员管理，请重新选择其他店铺";
                            break;

                        }
                        else
                        {
                            if (managerStoreBll.Add(msModel) > 0)
                            {
                                if (bll.Add(model) > 0)
                                {
                                    msg = "添加成功";
                                    jieguo = 1;
                                }


                            }
                            else
                            {
                                jieguo = 1;
                                msg = "添加失败";

                            }
                        }
                    }

                }
                else
                {
                    jieguo = 0;
                    msg = "该账号已经存在，请重新填写";

                }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";

            }
            return Json(new { jieguo = jieguo, state = result, msg = msg });
        }




        public ActionResult Save2(manager model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            string code = model.username;
            string pwd = model.password;

            if (model.username != "" && model.password != "" && model.username != null && model.password != null)
            {
                var count = bll.Search(x => x.username == code).Count();
                //判断该用户是否存在
                if (count == 0)
                {
                    model.password = MD5Encrypt32(pwd);
                    model.state = 1;
                    model.RealName = model.RealName;
                    model.username = model.username;
                    model.userPhone = model.userPhone;
                            if (bll.Add(model) > 0)
                            {
                                msg = "添加成功";
                                jieguo = 1;
                            }
                        else
                        {
                            jieguo = 1;
                            msg = "添加失败";

                        }
                    }
                else
                {
                    jieguo = 0;
                    msg = "该账号已经存在，请重新填写";

                }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";

            }
            return Json(new { jieguo = jieguo, state = result, msg = msg });
        }

        /// <summary>
        /// 修改 理货员相关信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public ActionResult Modify2(manager model)
        {
            string msg = "修改失败";
            string state = "false";
            model.password = MD5Encrypt32(model.password);
            string code = model.username;
            string pwd = model.password;

            model.state = 1;

            if (model.username != "" && model.password != "")
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                state = "false";
            }
            return Json(new { state = state, msg = msg });
        }

        //禁用用户  修改状态为
        public ActionResult ModifyState(long id)
        {
            string msg = "禁用失败";
            string state = "false";

            var oneList = bll.Search(x => x.userid == id).Select(x => new { x.password, x.username, x.RealName, x.userPhone, x.state, x.userid }).ToList();
            manager model = new manager();
            model.userid = oneList.FirstOrDefault().userid;
            model.password = oneList.FirstOrDefault().password;
            model.username = oneList.FirstOrDefault().username;
            model.userPhone = oneList.FirstOrDefault().userPhone;
            model.RealName = oneList.FirstOrDefault().RealName;
            model.state = 0;

            if (bll.Modify(model) > 0)
            {
                msg = "禁用成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }
        //获取选中的一条数据
        public ActionResult GetOnlyOne(long id)
        {
            return Json(bll.Search(x => x.userid == id).ToList().Select(x => new { x.password, x.username, x.userid, x.RealName, x.userPhone, x.state }).ToList()[0], JsonRequestBehavior.AllowGet);
        }


        //MD5加密方法  32位加密
        public static string MD5Encrypt32(string password)
        {
            string cl = password;
            string pwd = "";
            MD5 md5 = MD5.Create(); //实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("X2");
            }
            return pwd;
        }



        StoreBll storeBll = new StoreBll();

        public class StroeModel
        {
            public int storeid;
            public string storeName;
            public string property;
            public string storeDress;
            public string discount;

        }

        // 获取该理货员 管理的 店铺列表 + 
        public ActionResult GetStoresByID(int userid)
        {
            var StroeModelList = new List<StroeModel>();
            var storeIdList = managerStoreBll.Search(x => x.managerCode == userid).Select(x => new { x.storeid, }).ToList();
            foreach (var item in storeIdList)
            {
                if (item.storeid == "*")
                {
                    var storeList = storeBll.Search(x => x.storeStatus == 1).Select(x => new { x.storeid, x.storeName, property = x.property == 0 ? "校园" : x.property == 1 ? "园区" : "社区", x.storeDress, x.discount }).ToList();
                    StroeModel storeModel = new StroeModel();
                    if (storeList.Count != 0)
                    {
                        storeModel.discount = storeList.FirstOrDefault().discount.ToString();
                        storeModel.property = storeList.FirstOrDefault().property;
                        storeModel.storeDress = storeList.FirstOrDefault().storeDress;
                        storeModel.storeid = storeList.FirstOrDefault().storeid;
                        storeModel.storeName = storeList.FirstOrDefault().storeName;
                        StroeModelList.Add(storeModel);

                    }
                }
                else
                {
                    int storeId = Convert.ToInt32(item.storeid);
                    var storeList = storeBll.Search(x => x.storeid == storeId && x.storeStatus == 1).Select(x => new { x.storeid, x.storeName, property = x.property == 0 ? "校园" : x.property == 1 ? "园区" : "社区", x.storeDress, x.discount }).ToList();
                    StroeModel storeModel = new StroeModel();
                    if (storeList.Count != 0)
                    {
                        storeModel.discount = storeList.FirstOrDefault().discount.ToString();
                        storeModel.property = storeList.FirstOrDefault().property;
                        storeModel.storeDress = storeList.FirstOrDefault().storeDress;
                        storeModel.storeid = storeList.FirstOrDefault().storeid;
                        storeModel.storeName = storeList.FirstOrDefault().storeName;
                        StroeModelList.Add(storeModel);

                    }
                }

            }
            return Json(StroeModelList);
        }

        public ActionResult GetShop()
        {
            return Json(storeBll.Search(x=>x.storeStatus==1).Select(x => new { shopID = x.storeid, shopName = x.storeName }).ToList());
        }
    }
}
